﻿Option Explicit On
Option Strict On
Option Compare Text

Imports System.Drawing
Imports System.Windows.Forms

Partial Class CtlLightGPSTrack
    Inherits UserControl

    '##########################################################################
    '       Etat du GPS
    '##########################################################################
    'Déclaration de la variable et de la liste des valeurs possibles

    Private mGpsStatus As GpsLightStatus

    Public Enum GpsLightStatus
        statusOff = 0 'fermé
        statusOn = 1 'ouvert avec reception
        statusOnWithoutSignal = 2 'ouvert sans reception de signal
    End Enum

    'Déclaration d'un évenement qui detecte le changement de valeur du status
    Public Event GpsStatusChanged(ByVal NewStatus As GpsLightStatus)

    'Décalaration accesseurs
    Public Property GpsStatus() As GpsLightStatus
        Get
            mGpsStatus = mGpsStatus
        End Get
        Set(ByVal Value As GpsLightStatus)
            If mGpsStatus <> Value Then
                mGpsStatus = Value
                'Si le GPS n'est pas ouvert alors le track ne peut être enregistré
                If Value = GpsLightStatus.statusOff Then
                    mTrackStatus = TrackLightStatus.statusOff
                End If
                RaiseEvent GpsStatusChanged(mGpsStatus)
                Me.Invalidate()
            End If
        End Set
    End Property

    '##########################################################################
    '       Etat du Track : en cours de sauvegarde ou non
    '##########################################################################
    'Déclaration de la variable et de la liste des valeurs possibles
    Dim mTrackStatus As TrackLightStatus = TrackLightStatus.statusOff

    Public Enum TrackLightStatus
        statusOn = 1 ' en cours d'enregistrement
        statusOff = 0 'pas d'enregistrement
    End Enum

    'Déclaration d'un évenement qui detecte le changement de valeur du status
    Public Event TrackStatusChanged(ByVal NewStatus As TrackLightStatus)

    'Décalaration accesseurs
    Public Property TrackStatus() As TrackLightStatus
        Get
            TrackStatus = mTrackStatus
        End Get
        Set(ByVal Value As TrackLightStatus)
            If mTrackStatus <> Value Then
                'Si le GPS n'est pas ouvert alors le track ne peut être enregistré
                If mGpsStatus = GpsLightStatus.statusOff Then
                    mTrackStatus = TrackLightStatus.statusOff
                Else
                    mTrackStatus = Value
                End If
                RaiseEvent TrackStatusChanged(mTrackStatus)
                Me.Invalidate()
            End If
        End Set
    End Property

    '#################################################################
    ' Réinitialisation du composant = Tout à off
    Public Sub ResetStatus()
        Me.GpsStatus = GpsLightStatus.statusOff
        Me.TrackStatus = TrackLightStatus.statusOff
    End Sub


    Public Function ShouldSerializeGPSStatus() As Boolean
        If mGpsStatus = GpsLightStatus.statusOff Then
            Return False
        Else
            Return True
        End If
    End Function

    Public Function ShouldSerializeTrackStatus() As Boolean
        If mTrackStatus = TrackLightStatus.statusOff Then
            Return False
        Else
            Return True
        End If
    End Function

    '############################################################################
    ' Méthodes qui vont permettre l'affichage de l'état de track et GPS
    '############################################################################
    'Dession de l'ensemble du control
    Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(pe)

        Dim grfGraphics As System.Drawing.Graphics
        grfGraphics = pe.Graphics

        ' Dessiner d'abord deux cercles qui symbolise l'état de GPS et Track
        DrawLight(mGpsStatus, mTrackStatus, grfGraphics)

        ' Mettre à disposition les objets de dessin
        grfGraphics.Dispose()

    End Sub

    Private Sub DrawLight(ByVal GPSLightToDraw As GpsLightStatus, ByVal TrackLightToDraw As TrackLightStatus, _
     ByVal grfGraphics As Graphics)

        Dim nCircleX As Integer
        Dim nCircleY As Integer
        Dim nCircleDiameter As Integer
        Dim nCircleColor As Color
        Dim bshBrush As System.Drawing.Brush
        Dim blackPen As System.Drawing.Pen
        blackPen = New Pen(Color.Black, 1.5)

        nCircleX = 0
        nCircleDiameter = CInt(Me.Size.Width - (Me.Size.Width * 0.2))

        'Dessiner les différents cercle
        'GPS
        If GPSLightToDraw = GpsLightStatus.statusOff Then
            nCircleColor = Color.Red
        ElseIf GPSLightToDraw = GpsLightStatus.statusOnWithoutSignal Then
            nCircleColor = Color.Orange
        Else
            nCircleColor = Color.Green
        End If
        nCircleY = CInt(Me.Size.Width + (Me.Size.Width * 0.1))
        bshBrush = New SolidBrush(nCircleColor)
        ' Dessiner le cercle pour le cercle GPS
        'contour
        grfGraphics.DrawEllipse(blackPen, nCircleX, nCircleY, nCircleDiameter, nCircleDiameter)
        'intérieur
        grfGraphics.FillEllipse(bshBrush, nCircleX, nCircleY, nCircleDiameter, nCircleDiameter)

        'Track
        If TrackLightToDraw = TrackLightStatus.statusOff Then
            nCircleColor = Color.Red
        Else
            nCircleColor = Color.Green
        End If
        nCircleY = 0 + CInt(Me.Size.Width * 0.1)
        bshBrush = New SolidBrush(nCircleColor)
        ' Dessiner le cercle pour le cercle Track
        'contour
        grfGraphics.DrawEllipse(blackPen, nCircleX, nCircleY, nCircleDiameter, nCircleDiameter)
        'intérieur
        grfGraphics.FillEllipse(bshBrush, nCircleX, nCircleY, nCircleDiameter, nCircleDiameter)

        ' Mettre l'objet Brush à disposition
        bshBrush.Dispose()

    End Sub

    Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
        'CtlLightGPSTrack
        '
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit
        Me.BackColor = System.Drawing.SystemColors.Window
        Me.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Name = "CtlLightGPSTrack"
        Me.Size = New System.Drawing.Size(10, 22)
        Me.ResumeLayout(False)

    End Sub
End Class
